package org.ruanjj.multithread;
//模拟卖票
public class WindowTest {
    public static void main(String[] args) throws InterruptedException {
        SaleTickets saleTickets = new SaleTickets();
        Thread t1 = new Thread(saleTickets,"售票窗口1");
        t1.setDaemon(true);
        t1.start();


        Thread.sleep(4000);
     /*   Thread t2 = new Thread(saleTickets,"售票窗口2");
        t2.start();
        Thread t3 = new Thread(saleTickets,"售票窗口3");
        t3.start();
        Thread t4 = new Thread(saleTickets,"售票窗口4");
        t4.start();
*/
    }
}



//给同步代码块加锁
class SaleTickets implements Runnable{
    private int tickets = 100;

    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            synchronized (SaleTickets.class){
                if(tickets > 0){
                    System.out.println(Thread.currentThread().getName() + "售票" + tickets);
                    tickets--;
                }else{
                    break;
                }
            }
        }
    }
}